package com.jt.controller;

import com.jt.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    /**
     * 对象方式接收参数
     */



    /**
     * 测试@RequestParam注解
     * 1. name/value  参数名
     * 2. required
     * 3. defaultValue  默认值
     */
//    @RequestMapping("/addUser")
    public String addUserParam(@RequestParam Integer id, String name,String... hobbies){
        //内部通过传过来的参数名调用getParameter，类型使用参数类型
        System.out.println(id+":"+name);
        for (String hobby : hobbies) {
            System.out.println("hobby = " + hobby);
        }
        return "success";
    }

    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println("user = " + user);
        return "success";
    }

    /**
         * mvc底层数据传输原则
         * ModelAndView:
         *  1.model 数据
         *  2.view  页面
         *  handler处理器真正执行时，方法才会被调用
         */
//        @RequestMapping("/user")
//        public ModelAndView toUser(){
//            ModelAndView modelAndView = new ModelAndView();
//            modelAndView.addObject("id", 1001);
//            modelAndView.addObject("name", "测试");
//            modelAndView.setViewName("user");
//            return modelAndView;
//        }
        //简化
        @RequestMapping("/user")
        public String  toUser(Model model){
            model.addAttribute("id", 101);
            model.addAttribute("name", "小明");
            return "user";
        }

    /**
     *
     * @param
     * @return
     */
//    @RequestMapping("/addUser")
        public String addUser(@RequestParam Integer id, String name){
        //内部通过传过来的参数名调用getParameter，类型使用参数类型
        System.out.println(id+":"+name);
        return "success";
    }
}
